
package com.kunbo.cn.service.impl.core.card;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.card.ICCardDao;
import com.kunbo.cn.dao.core.customer.CustomerDataPermissionsDao;
import com.kunbo.cn.entity.core.card.ICCard;
import com.kunbo.cn.entity.core.handset.HandsetUsers;
import com.kunbo.cn.service.core.card.ICCardService;
import com.kunbo.cn.util.date.SDate;

/**
 *
 * @author bjl  
 * @date 2019年3月21日 
 */
@Service
public class ICCardServiceImpl implements ICCardService {
	@Autowired
	private ICCardDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	
	@Override
	public void save(ICCard ic) {
		ic.setRegister_time(SDate.getSystemDateToString());
		dao.save(ic);
	}

	
	@Override
	public void update(ICCard ic) {
		dao.save(ic);
	}

	@Override
	public void delete(String id) {
		dao.delete(id);
	}

	
	@Override
	public ICCard findById(String id) {
		return dao.findOne(id);
	}


	@Override
	public Page<ICCard> findICCard(String bill_code, String ic_code, String card_serial, Integer pageNo, Integer pageSize) {
		Pageable pageable = new PageRequest(pageNo-1, pageSize);
        Specification<ICCard> confusion=new Specification<ICCard>() {
            @Override
            public Predicate toPredicate(Root<ICCard> root, CriteriaQuery<?> query,
                    CriteriaBuilder criteriaBuilder) {
            	List<Predicate> predicates = new ArrayList<>();
            	if(!"".equals(bill_code)) {
            		predicates.add(criteriaBuilder.like(root.get("bill_code"), "%"+bill_code+"%"));
            	}
            	if(!"".equals(ic_code)) {
            		predicates.add(criteriaBuilder.like(root.get("ic_code"), "%"+ic_code+"%"));
            	}
            	if(!"".equals(card_serial)) {
            		predicates.add(criteriaBuilder.like(root.get("card_serial"), "%"+card_serial+"%"));
            	}
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return dao.findAll(confusion, pageable);
	}

}
